package com.suarte.webapp.servlet;

import com.suarte.core.Product;
import com.suarte.core.service.ProductManager;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
 * @date Jan 10th, 2010
 * @author Smahmud
 */
public class ProductSearch extends HttpServlet {
    private ProductManager productManager;
    private ApplicationContext appContext;

    static class Response {

        boolean success;
        String message;

        public Response() {
            success = true;
        }

        public Response(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }
    

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void activate(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (appContext == null) {
            appContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            productManager = (ProductManager) appContext.getBean("productManager");
        }
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
        response.setHeader("Pragma", "no-cache"); //HTTP 1.0
        response.setDateHeader("Expires", 0); //prevents caching at the proxy server

        String description = request.getParameter("autocomplete_parameter");
        //String code = request.getParameter("autocomplete_parameter2");
        List<Product> products = null;
         
        if (!isBlank(description)) {
          products = productManager.findByDescription(description.toUpperCase());
        }
//        if (!isBlank(code)) {
//            products = productManager.findByCode(code);
//        }
        
        response.getWriter().append("<ul>");
        if (products.isEmpty()) {
            response.getWriter().append("<li>No hay registros</li>");
        } else {
            for (Product product : products) {
                response.getWriter().append("<li id=\"" + product.getId() + "\">" + product.getDescription() + "-" + product.getPrice() + "</li>");
            }
        }
        response.getWriter().append("</ul>");
        response.getWriter().flush();
        response.getWriter().close();

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /** Returns a short description of the servlet.
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
